package com.lzkjiot.service.gnss.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.fusesource.mqtt.client.Future;
import org.fusesource.mqtt.client.FutureConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class DeviceMqttClient {

    private FutureConnection createConnection(String mqttServerUrl, int mqttServerPort, String userName) throws Exception {
        MQTT mqtt = new MQTT();
        FutureConnection connection;
        mqtt.setHost(mqttServerUrl, mqttServerPort);
        mqtt.setUserName(userName);
        connection = mqtt.futureConnection();
        Future<Void> f1 = connection.connect();
        f1.await();
        return connection;
    }

    public boolean publishAttributes(String mqttServerUrl, int mqttServerPort, String userName, Map<String, Object> data){
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.valueToTree(data);
        FutureConnection connection = null;
        try {
            connection = createConnection(mqttServerUrl, mqttServerPort, userName);
        } catch (Exception e) {
            log.error("设备创建mqtt连接失败：{}", e.getMessage());
            return false;
        }
        Future<Void> futurePublishAttrs = connection.publish("v1/devices/me/attributes", jsonNode.toString().getBytes(),
                QoS.AT_MOST_ONCE, false);
        try {
            futurePublishAttrs.await();
            log.info("推送设备属性数据成功.参数：{}", jsonNode.toString());
            return true;
        } catch (Exception e) {
            log.error("推送设备属性数据失败：{}.参数：{}", e.getMessage(), jsonNode.toString());
        }finally {
            Future<Void>  future = connection.disconnect();
            try {
                future.await();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("释放推送设备属性mqtt连接失败：{}.", e.getMessage());
            }
        }
        return false;
    }

    public boolean publishTelemetry(String mqttServerUrl, int mqttServerPort, String userName, Map<String, Object> data){
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.valueToTree(data);
        FutureConnection connection = null;
        try {
            connection = createConnection(mqttServerUrl, mqttServerPort, userName);
        } catch (Exception e) {
            log.error("设备创建mqtt连接失败：{}", e.getMessage());
            return false;
        }
        Future<Void> futurePublishAttrs = connection.publish("v1/devices/me/telemetry", jsonNode.toString().getBytes(),
                QoS.AT_MOST_ONCE, false);
        try {
            futurePublishAttrs.await();
            log.info("推送设备遥测数据成功.参数：{}", jsonNode.toString());
            return true;
        } catch (Exception e) {
            log.error("推送设备遥测数据失败：{}.参数：{}", e.getMessage(), jsonNode.toString());
        }finally {
            Future<Void>  future = connection.disconnect();
            try {
                future.await();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("释放推送设备遥测数据mqtt连接失败：{}.", e.getMessage());
            }
        }
        return false;
    }
}
